import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor,
  HttpResponse,
  HttpErrorResponse
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { tap } from 'rxjs/operators';

@Injectable()
export class HttpInterceptors implements HttpInterceptor {

  constructor(router: ActivatedRoute) {}

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    /*
    */
    console.log("Before Request");
    const modifiedRequest = request.clone({
      headers: request.headers.append('Authorization', `Bearer`)
    });
    return next.handle(modifiedRequest).pipe(tap(
      (event: HttpEvent<any>) => {
        if(event instanceof HttpResponse) {

        }
      }, (error: HttpErrorResponse) => {
        if(error.status === 404) {
          console.log("This url doesn't exist");
        }
      }
    ));
  }
}
